package com.his.struts.dao.ry;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.his.struts.dao.question.QuestionImp;
import com.his.struts.form.RyForm;
import com.his.struts.util.DbUtil;

/**
 * 实现荣誉接口类
 * 
 * @author liumiaoyue
 * 
 */
public class RyImp implements RyDao {
	Logger log = Logger.getLogger(RyImp.class);
	DbUtil db = new DbUtil();

	/**
	 * 得到所有荣誉
	 * 
	 * @return List<RyForm> 荣誉form集合
	 */
	public List<RyForm> getAllRy() {
		List<RyForm> list = new ArrayList<RyForm>();
		String sql = "select * from rongyu order by ryId desc";
		ResultSet rs = db.executeQuery(sql, null);
		try {
			while (rs.next()) {
				RyForm form = new RyForm();
				int ryId = rs.getInt("ryId");
				String ryInfo = rs.getString("ryInfo");
				String ryPic = rs.getString("ryPic");
				form.setRyId(ryId);
				form.setRyInfo(ryInfo);
				form.setRyPic(ryPic);
				list.add(form);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return list;
	}

	/**
	 * 得到所有荣誉
	 * 
	 * @return List<RyForm> 荣誉form集合
	 */
	public List<RyForm> getRyBean(ResultSet rs) {
		List<RyForm> list = new ArrayList<RyForm>();
		try {
			while (rs.next()) {
				RyForm form = new RyForm();
				int ryId = rs.getInt("ryId");
				String ryInfo = rs.getString("ryInfo");
				String ryPic = rs.getString("ryPic");
				form.setRyId(ryId);
				form.setRyInfo(ryInfo);
				form.setRyPic(ryPic);
				list.add(form);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		db.closeAll();
		return list;
	}

	/**
	 * 删除荣誉
	 * 
	 * @param ryId
	 *            荣誉id
	 * @return boolean
	 */
	public boolean delRy(int ryId) {
		boolean ok = false;
		ArrayList list = new ArrayList();
		String sql = "delete from rongyu where ryId=?";
		list.add(ryId);
		int i = db.executeUpdate(sql, list);
		if (i != 0) {
			ok = true;
		}
		db.closeAll();
		return ok;
	}

	/**
	 * 更新荣誉
	 * 
	 * @param form
	 *            荣誉form
	 * @return boolean
	 */
	public boolean updateRy(RyForm form) {
		boolean ok = false;
		ArrayList list = new ArrayList();
		String sql = "update rongyu set ryInfo=? where ryId=?";
		list.add(form.getRyInfo());
		if(form.getRyPic()!=null&&!"".equals(form.getRyPic())){
			sql = "update rongyu set ryInfo=?,ryPic=? where ryId=?";
			list.add(form.getRyPic());
		}
		list.add(form.getRyId());
		int i = db.executeUpdate(sql, list);
		if (i != 0) {
			ok = true;
		}
		db.closeAll();
		return ok;
	}

	/**
	 * 添加荣誉
	 * 
	 * @param form
	 *            荣誉form
	 * @return boolean
	 */
	public boolean addRy(RyForm form) {
		boolean ok = false;
		ArrayList list = new ArrayList();
		String sql = "insert into rongyu(ryInfo,ryPic) values(?,?)";
		list.add(form.getRyInfo());
		list.add(form.getRyPic());
		int i = db.executeUpdate(sql, list);
		if (i != 0) {
			ok = true;
		}
		db.closeAll();
		return ok;
	}

}
